package com.yc.url;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;

public class ZpwSpider {
    public static void main(String[] args) throws IOException {
        ZpwSpider zs=new ZpwSpider();

      //zs.capJob("https://www.0734zpw.com/j46486.html");
        for (int i = 1; i <= 5; i++) {
            zs.capJobs("http://www.0734zpw.com/jobs?page="+i);
        }
    }


    //收集 job 链接
    public void capJobs(String address) throws IOException {
        URL url=new URL(address);
        Document document = Jsoup.parse(url, 5000);
        Elements as= document.select(".td-j-name>a");
        for(Element element:as){
            //
            String href=element.attr("href");
            String jobAddress=url.getProtocol()+"://"+url.getHost()+href;
            capJob(jobAddress);
        }

    }




    public void capJob(String address) throws IOException {
        DBHelper db=new DBHelper();
        String id= address.replaceAll(".*j(\\d+)\\.html","$1");
        System.out.println(id);
        String sql1="select count(*) cnt from job where id = ?";
        List<Map<String, Object>> list= db.findMultiple(sql1,id);
        if(Integer.parseInt(""+list.get(0).get("cnt"))>0){
            System.out.println("该 job"+id+"已经采集过了");
            return;
        }





        Document document = Jsoup.parse(new URL(address), 5000);

        Element element = document.selectFirst(".jobname>.j-n-txt");
        System.out.println(element);
        String jobname = element.text();
        element= document.selectFirst(".jobstit>.wage");
        String money = element.text();


        Elements  elements= document.select(".main>.item>.itemli");
        String type = elements.get(0).text();
        type = type.replaceAll("学历要求(.+)", "$1");

        String age = elements.get(1).text();
       age = type.replaceAll("年龄要求(.+)", "$1");

     element= document.selectFirst(".main>.describe>.df2");
        String describe = element.html();



        System.out.println("jobname = " + jobname);
        System.out.println("money = " + money);
        System.out.println("type = " + type);
        System.out.println("age = " + age);
        System.out.println("describe = " + describe);



        String sql="insert into job values(?,?,?,?,?,?)";
       db.update(sql,id,jobname,money,type,age,describe);
    }

}
